package services;

import dao.TruckInfo_Repository;
import models.*;
import org.apache.commons.collections.CollectionUtils;
import play.db.jpa.JPAApi;

import javax.inject.Inject;
import javax.persistence.Query;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.supplyAsync;


/**
 * Provide JPA operations running inside of a thread pool sized to the connection pool
 */
public class TruckInfo_Repository_JPA implements TruckInfo_Repository {

    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public TruckInfo_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }

    @Override

    public CompletionStage<List<VehicleInfo_P_Entity>> getTruckPlace (String truckNumber)
    {
    	return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                String strSql = "EXEC p_GetTruckLoca @PlateNo = '"+truckNumber+"'";
                return entityManager.createNativeQuery(strSql, VehicleInfo_P_Entity.class).getResultList();
                });
        }, executionContext);    	
    }

    @Override
    public CompletionStage<String> boundTruckAndQRcode(String truckNumber,String GPSId,String SimNo,String Industry,String phoneNumber,String latitude,String longitude)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                if(GPSId==null || "".equals(GPSId) || "null".equals(GPSId)){
                    return "请扫描或者输入GpsId";
                    }

                if(SimNo==null || "".equals(SimNo) || "null".equals(SimNo)){
                    return "请扫描或者输入SimNo";
                }
                                 if(longitude==null|| "".equals(longitude)){
                 return "请开启手机gps定位";
                 }
                Query query = entityManager.createQuery("select u from VehicleEntity u where u.simNo = :simNo", VehicleEntity.class).setParameter("simNo", SimNo);
                if(query.getResultList().size() > 0)
                {

                    return "请勿添加重复SimNo";
                }
                String strSql= "EXEC p_AddTrcukGPS @PhoneNum = '"+phoneNumber+"',@PlateNo = '"+truckNumber+"',@GPSId = '"+GPSId+"',@SimNo = '"+SimNo+"',@Industry = '"+Industry+"',@Lat = '"+latitude+"',@Lng = '"+longitude+"'";
                List<Object>  list= entityManager.createNativeQuery(strSql).getResultList();

                Object[] log = (Object[]) list.get(0);
                    if(log.length>3){

                        return log[5].toString();
                    }else {
                        return "设备绑定成功";


                    }


            });
        }, executionContext);
    }


    public CompletionStage<List<Truck_P_Entity>> getTruckList (Integer GPSFlag)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                String strSql = "EXEC p_GetTruckList @GPSFlag = '"+GPSFlag+"'";
                return entityManager.createNativeQuery(strSql, Truck_P_Entity.class).getResultList();
            });
        }, executionContext);
    }
}